package com.lakers.notice.springboot;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.lakers.notice.springboot.help.NoticeMessageHelp;
import com.lakers.notice.springboot.model.NoticeMessageInfo;
import com.lakers.notice.springboot.model.NoticeMessageQueryInfo;
import com.lakers.notice.springboot.model.NoticePageResult;
import com.lakers.notice.springboot.model.NoticeSendMessage;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述: NoticeClient
 *
 * @author liqin(zxl)
 * @date 2024/5/3
 */
public class NoticeClient {

    @Resource
    private NoticeMessageHelp noticeMessageHelp;

    /**
     * 发送消息
     * @param message 消息体
     * @see  NoticeSendMessage
     */
    public void send(NoticeSendMessage message){
        message.validation();
        final Long messageId = noticeMessageHelp.save(message);
        noticeMessageHelp.trySave(message.getSender().getUserId(), ListUtil.of(messageId));
        noticeMessageHelp.send(message,String.valueOf(messageId));
    }

    /**
     * 阅读消息
     * @param userId 用户ID
     * @param messageIds 消息ID集合
     * @return true/false
     */
    public Boolean read(String userId, List<Long> messageIds){
        noticeMessageHelp.trySave(userId,messageIds);
        return Boolean.TRUE;
    }


    /**
     * 获取未读消息数量
     * @param userId 用户ID
     * @return 未读数量
     */
    public Integer getUnReadCount(String userId){
       return noticeMessageHelp.unReadCount(userId);
    }


    /**
     * 获取消息列表
     * @param messageQuery 消息查询条件
     * @see NoticeMessageQueryInfo
     * @return {@link NoticeMessageInfo}
     */
    public NoticePageResult<NoticeMessageInfo> getMessages(NoticeMessageQueryInfo messageQuery){
        if (!messageQuery.validation()) {
            throw new RuntimeException("user id is null, query request is:" + JSONUtil.toJsonStr(messageQuery));
        }
        return noticeMessageHelp.getMessages(messageQuery);
    }
}
